//
//  VideoOneViewController.m
//  project_delicious
//
//  Created by dlios on 15-4-4.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "VideoOneViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface VideoOneViewController ()
@property (nonatomic, retain) MPMoviePlayerController *moviePlayer;
@property (nonatomic, retain) UIAlertView *alertView;


@end



@implementation VideoOneViewController
- (void)dealloc
{
    [_alertView release];
    [_moviePlayer release];
    [_strVideo1 release];
    [super dealloc];
}
- (NSURL *)strChangeUrl:(NSString *)str
{
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    return url;
}
- (NSString *)GetCurrntNet
{
    NSString *result;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            return nil;
            break;
        case ReachableViaWWAN://使用3G网络
            result = @"3G";
            break;
        case ReachableViaWiFi://wifi
            result = @"WIFI";
            break;
        default:
            break;
    }
    return result;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self GetCurrntNet] == nil) {
        NSString *message = [NSString stringWithFormat:@"网络状况不好, 请检查您的网络"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"网络" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        [self.view addSubview:alertView];
        [alertView release];
    } else if ([[self GetCurrntNet] isEqualToString:@"3G"]) {
        NSLog(@"当前使用的是3G网络");
        
    } else if ([[self GetCurrntNet] isEqualToString:@"WIFI"]) {
        NSLog(@"当前使用的是WIFI网络");
        
    }
    NSLog(@"当前使用的是%@", [self GetCurrntNet]);
    
    NSString *str = self.strVideo1;
    NSLog(@"%@", str);
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self strChangeUrl:self.strVideo1]];
    
    UILabel *matLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    matLabel.text = @"制作过程";
    matLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:matLabel];
    [matLabel release];
    
    self.moviePlayer.view.frame = CGRectMake(10, 30, WIDTH - 20, HEIGHT / 3);
    self.moviePlayer.view.layer.cornerRadius = 0;
    self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    [self.view addSubview:self.moviePlayer.view];
    [_moviePlayer release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, HEIGHT / 3 + 40, 100, 30);
    [button setTitle:@"播放视频" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    
    
}
- (void)buttonAction:(UIButton *)button
{
    if ([self GetCurrntNet] == nil) {
        NSString *message = [NSString stringWithFormat:@"网络状况不好, 请检查您的网络"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"网络" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
        [alertView show];
        [self.view addSubview:alertView];
        [alertView release];
    } else if ([[self GetCurrntNet] isEqualToString:@"3G"]) {
        NSLog(@"当前使用的是3G网络");
        self.alertView = [[UIAlertView alloc] initWithTitle:@"网络" message:@"当前使用3G网络, 是否播放视频" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [_alertView show];
        [self.view addSubview:_alertView];
        [_alertView release];
    } else {
        [self.moviePlayer play];
    }
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    } else if (buttonIndex == 1) {
        [self.moviePlayer play];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
